package com.rbac.api.controller.lessOne;


import com.rbac.api.controller.request.PostMenuAddRequest;
import com.rbac.api.controller.request.PostMenuSaveStatusRequest;
import com.rbac.api.controller.request.PostMenusListRequest;
import com.rbac.api.service.MenusCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


///菜单相关controller
@RestController
public class MenusController {


    ///菜单相关操作service
    @Autowired
    private MenusCommonService menusCommonService;


    ///获取拥有的菜单列表
    @ResponseBody
    @RequestMapping(value = "/admin/common/menus/getHaveList",method = RequestMethod.POST)
    public Object getHaveMenusListByToken(HttpServletRequest request) throws Exception{
        ///
        return menusCommonService.getHaveListByTokenService(request);
    }




    ///获取菜单列表
    @ResponseBody
    @RequestMapping(value = "/admin/check/menus/getList",method = RequestMethod.GET)
    public Object getMenusList(@Valid PostMenusListRequest menusListRequest, BindingResult bindingResult) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return menusCommonService.getMenusListService(menusListRequest);
    }



    ///新增菜单（增加）
    @ResponseBody
    @RequestMapping(value = "/admin/common/menus/add",method = RequestMethod.POST)
    public Object menuAdd(@Valid @RequestBody PostMenuAddRequest menuAddRequest, BindingResult bindingResult, HttpServletRequest request) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return menusCommonService.menuAddService(menuAddRequest,request);
    }



    ///新增菜单（获取顶级菜单列表）
    @ResponseBody
    @RequestMapping(value = "/admin/common/menus/getTopMenuList",method = RequestMethod.POST)
    public Object getTopMenuList() throws Exception{
        ///
        return menusCommonService.getTopMenuListService();
    }




    ///菜单上线下线
    @ResponseBody
    @RequestMapping(value = "/admin/common/menus/saveStatus",method = RequestMethod.POST)
    public Object menuSaveStatus(@Valid @RequestBody PostMenuSaveStatusRequest menuSaveStatusRequest, BindingResult bindingResult, HttpServletRequest request) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return menusCommonService.menuSaveStatusService(menuSaveStatusRequest,request);
    }









}
